package com.kajespero.dao.commons.impl;

import com.google.appengine.api.datastore.Key;
import com.kajespero.dao.commons.ReadWriteDAO;
import com.kajespero.model.IdentifierVOBean;
import com.kajespero.vo.IdentifierVO;

public abstract class AbstractReadWriteDAO<T extends IdentifierVO<Id>, Id extends Comparable<Id>> extends AbstractReadDAO<T, Id> implements ReadWriteDAO<T, Id>{

	@Override
	public void saveOrUpdate(T instance) {
		Key key = ((IdentifierVOBean<Id>)instance).getKey();
		if(key == null){
			key = createKey(instance.getId());
			((IdentifierVOBean<Id>)instance).setKey(key);
		}
		getIntance().saveOrUpdare(instance);
	}

	@Override
	public void delete(T instance) {
		getIntance().delete(instance);
	}

	@Override
	public void delete(Id identifier) {
		getIntance().delete(getBeanClass(), identifier);
	}

	@Override
	public abstract T createNewBean() throws InstantiationException, IllegalAccessException;
}
